<?php

class BepController {

    public function invoke() {
        $dir = dirname(__FILE__);

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }

        // tác vụ
        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }

        if ($action == 'remove') {
            // mảng danh sách giá trị id
            $values = $_POST["id"];

            $result = $this->deleteBep($values);


            if ($result == OK) {
//                Utility::messageBox('Xóa thành công');
                echo'OK';
            }
            exit();
        } else if ($action == 'add') {
            $BEP_TEN_BEP = $_POST['BEP_TEN_BEP'];
            $BEP_IS_THUE = $_POST['BEP_IS_THUE'];
            $result = $this->addBep($BEP_TEN_BEP, $BEP_IS_THUE);
            if ($result == OK) {
                if ($action == 'add') {
                    echo 'OK';
                } else {
                    Utility::redirect("./?com=com_bep");
                }
            }
            exit();
        } else if ($action == 'edit') {
            $BEP_ID = $_POST['BEP_ID'];
            $BEP_TEN_BEP = $_POST['BEP_TEN_BEP'];
            $BEP_IS_THUE = $_POST['BEP_IS_THUE'];
            $result = $this->updateBep($BEP_ID, $BEP_TEN_BEP, $BEP_IS_THUE);
            if ($result == OK) {
                if ($action == 'edit') {
                    echo 'OK';
                } else {
                    Utility::redirect("./?com=com_bep");
                }
            }
            exit();
        }


        if ($task == 'add') {
            $xtemplate = new XTemplate('./components' . DS . 'com_bep' . DS . 'views' . DS . 'bep_add.php');

            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
        // nếu task là edit thì include trang thêm vào
        else if ($task == 'edit') {

            $id = $_GET['idbep'];
            $xtemplate = new XTemplate('./components' . DS . 'com_bep' . DS . 'views' . DS . 'bep_edit.php');
            $bep = $this->getBepById($id);
            if ($bep != null) {
                $xtemplate->assign('BEP_ID', $bep->getBEP_ID());
                $xtemplate->assign('BEP_TEN_BEP', $bep->getBEP_TEN_BEP());
                $xtemplate->assign('BEP_IS_THUE', $bep->getBEP_IS_THUE());
                $option_bep = '';
                if ($bep->getBEP_IS_THUE() == '0') {
                    $option_bep = "<option selected=selected value='0'>No</option><option  value='1'>Yes</option>";
                } else {
                    $option_bep = "<option  value='0'>No</option> <option  selected=selected value='1'>Yes</option>";
                }

                $xtemplate->assign('BEP_IS_THUE', $option_bep);
                $xtemplate->parse('main.bep');
            }
            $xtemplate->parse('main');
            $xtemplate->out('main');
        } else {
            $viewFile = $dir . '/views/bep.php';
            $xtemplate = new XTemplate($viewFile);
            $beps = $this->getAllBep();


            if ($beps != null) {
                foreach ($beps as $bep) {

                    $xtemplate->assign('BEP_ID', $bep->getBEP_ID());
                    $xtemplate->assign('BEP_TEN_BEP', $bep->getBEP_TEN_BEP());
                    if ($bep->getBEP_IS_THUE() == b'0') {
                        $xtemplate->assign('BEP_IS_THUE', 'Không');
                    } else {
                        $xtemplate->assign('BEP_IS_THUE', 'Có');
                    }

                    $xtemplate->parse('main.bep');
                }
            }
            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
    }

    private function getAllBep() {
        $db = new Database();
        try {
            $bep = new Bep();

            $result = $bep->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getBepById($id) {
        $db = new Database();
        try {
            $bep = new Bep();
            $bep->setBEP_ID($id);
            $result = $bep->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function addBep($BEP_TEN_BEP, $BEP_IS_THUE) {

        $db = new Database();
        try {

            $bep = new Bep();
            $bep->setBEP_TEN_BEP($BEP_TEN_BEP);
            $bep->setBEP_IS_THUE($BEP_IS_THUE);
            $bep->insert($db);

            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function updateBep($BEP_ID, $BEP_TEN_BEP, $BEP_IS_THUE) {

        $db = new Database();
        try {

            $bep = new Bep();
            $bep->setBEP_ID($BEP_ID);
            $bep->setBEP_TEN_BEP($BEP_TEN_BEP);
            $bep->setBEP_IS_THUE($BEP_IS_THUE);
            $bep->update($db);

            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function deleteBep($values) {
        $db = new Database();
        try {

            $bep = new Bep();
            $bep->setBEP_ID($values);
            $bep->delete($db);

            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

}
